package com.zf.bm.common.validator;

import com.zf.bm.modules.policy.entity.*;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * 根据比较结果添加验证范围验证
 *
 * @author zjy
 * @Date 2021/9/7 19:36
 */
public class RangeCompareSequenceValidator implements DefaultGroupSequenceProvider<PolicyBaseEntity> {
    @Override
    public List<Class<?>> getValidationGroups(PolicyBaseEntity policyBaseEntity) {
        List<Class<?>> list = new ArrayList<>();
        list.add(PolicyMultimediaFile.class);
        list.add(PolicyPictureFile.class);
        list.add(PolicyEncryptionFile.class);
        list.add(PolicyCompressFile.class);
        list.add(PolicyFileEncryption.class);
        list.add(PolicyFileAttributes.class);

        String minSizeString = "";
        String maxSizeString = "";
        if (policyBaseEntity == null) {
            return list;
        }
        if (policyBaseEntity instanceof PolicyMultimediaFile) {
            return getList((PolicyMultimediaFile) policyBaseEntity, list);
        }
        if (policyBaseEntity instanceof PolicyEncryptionFile) {
            minSizeString = ((PolicyEncryptionFile) policyBaseEntity).getMinSize();
            maxSizeString = ((PolicyEncryptionFile) policyBaseEntity).getMaxSize();
        } else if (policyBaseEntity instanceof PolicyPictureFile) {
            minSizeString = ((PolicyPictureFile) policyBaseEntity).getMinSize();
            maxSizeString = ((PolicyPictureFile) policyBaseEntity).getMaxSize();
        } else if (policyBaseEntity instanceof PolicyCompressFile) {
            minSizeString = ((PolicyCompressFile) policyBaseEntity).getBackSize();
            maxSizeString = ((PolicyCompressFile) policyBaseEntity).getDropSize();
        } else if (policyBaseEntity instanceof PolicyFileEncryption) {
            minSizeString = ((PolicyFileEncryption) policyBaseEntity).getMinSize();
            maxSizeString = ((PolicyFileEncryption) policyBaseEntity).getMaxSize();
        } else if (policyBaseEntity instanceof PolicyFileAttributes) {
            minSizeString = ((PolicyFileAttributes) policyBaseEntity).getMinSize();
            maxSizeString = ((PolicyFileAttributes) policyBaseEntity).getMaxSize();
            if (minSizeString != null && maxSizeString != null && !"".equals(minSizeString) && !"".equals(maxSizeString)) {
                if (Long.parseLong(minSizeString) >= Long.parseLong(maxSizeString)) {
                    list.add(IRangeCompare.class);
                }
            }
            return list;
        }

        if (minSizeString != null
                && maxSizeString != null
                && Long.parseLong(minSizeString) >= Long.parseLong(maxSizeString)) {
            list.add(IRangeCompare.class);
        }
        return list;
    }

    private static List<Class<?>> getList(PolicyMultimediaFile policyBaseEntity, List<Class<?>> list) {
        String maxSizeString;
        String minSizeString;
        minSizeString = policyBaseEntity.getMinSize();
        maxSizeString = policyBaseEntity.getMaxSize();
        if (minSizeString != null
                && (!maxSizeString.equals("0"))
                && (!minSizeString.equals("0"))
                && maxSizeString != null
                && Long.parseLong(minSizeString) > Long.parseLong(maxSizeString)) {
            list.add(IRangeCompare.class);
        }
        return list;
    }
}
